package com.yunhe.common.util;

import java.util.Optional;

public class StringUtil {

    public static boolean isEmpty(String str) {
        return str == null || "".equals(str) || "null".equals(str);
    }

    public static boolean isNumber(String str) {
        for(int index = 0; index < str.length(); ++index) {
            if ('0' > str.charAt(index) || str.charAt(index) > '9') {
                return false;
            }
        }

        return true;
    }

    public static <T> T getValue(Optional<T> param) {
        return param.orElse(null);
    }

    public static String escapeToMysql(String oldStr) {
        if (oldStr != null && oldStr.length() != 0) {
            oldStr = oldStr.replace("%", "\\%");
            oldStr = oldStr.replace("\\", "\\\\");
            oldStr = oldStr.replace("_", "\\_");
            return oldStr;
        } else {
            return oldStr;
        }
    }
}